﻿define(['services/datacontext', 'services/logger', 'durandal/plugins/router', 'services/helpers', 'config'],
    function (datacontext,logger,router,helpers,config) {
        var cars = ko.observableArray(),
            currentPage = ko.observable(0),
            pagesize = ko.observable(config.pagePerList),
            totalPages = ko.observable(),
            totalRecords = ko.observable();
        
        var apiMethod = '';
        
        //#region Internal Methods
        function activate(routeData) {
            switch(routeData.type)
            {
                case 'featured': 
                    apiMethod = 'GetFeaturedCars';
                    break;
                case 'latest':
                    apiMethod = 'GetNewCars';
                    break;
                default:
                    apiMethod = 'GetAllCars/' + routeData.menu.replace('menu-','') + '/' + routeData.model;
                    break;
                    
            }
            return datacontext.getCarsByPage(cars, apiMethod, currentPage, pagesize, totalPages, totalRecords, false);
        }

        var previous = function () {
            if (currentPage() > 0) {
                currentPage(currentPage() - 1);
                datacontext.getCarsByPage(cars, apiMethod, currentPage, pagesize, totalPages, totalRecords, false);
            }
        };

        var next = function () {
            if (currentPage() < totalPages() - 1) {
                currentPage(currentPage() + 1);
                datacontext.getCarsByPage(cars, apiMethod, currentPage, pagesize, totalPages, totalRecords, false);
            }
        };

        var currentlyViewed = ko.computed(function () {
            if (currentPage() !== undefined && totalPages() !== undefined && pagesize() !== undefined && totalRecords() !== undefined) {
                var str = currentPage() + 1 === 1 ? (currentPage() + 1).toString() : (currentPage() * pagesize() + 1).toString();
                if (currentPage() + 1 === totalPages()) {
                    return str + ' - ' + totalRecords().toString();
                
                } else {
                    return str + ' - ' + ((currentPage() + 1) * pagesize()).toString();
                }
            }
            return '';
        });

        var allPages = ko.computed(function () {
            
            var pages = [];
            var startingIndex = (currentPage() + 1) - 10 <= 0 ? 1 : (Math.floor(currentPage() / 10) * 10) + 1;
            var pageNumberDisplayed = 1;
            for (var i = startingIndex; i <= totalPages(); i++) {
                pages.push({
                    pageNumber: (i)
                });
                if (pageNumberDisplayed === 10) { break; }
                pageNumberDisplayed++;
            }
            return pages;

        });

        var moveToPage = function (selectedPage) {
            if (selectedPage.pageNumber !== currentPage() + 1) {
                currentPage(selectedPage.pageNumber - 1);
                datacontext.getCarsByPage(cars, apiMethod, currentPage, pagesize, totalPages, totalRecords, false);
            }
        };

        var viewAttached = function (view) {
            helpers.bindToEventList(view, '.view-link', gotoDetails, 'click');
            helpers.detectIDevice();
        };

        var gotoDetails = function(selectedCar) {
            if (selectedCar.id() !== null) {
                var url = '#/cardetails/' + selectedCar.id();
                router.navigateTo(url);
            }
        };


        var vm = {
            cars: cars,
            currentPage: currentPage,
            totalPages: totalPages,
            totalRecords: totalRecords,
            previous: previous,
            next: next,
            currentlyViewed: currentlyViewed,
            allPages: allPages,
            moveToPage:moveToPage,
            activate: activate,
            viewAttached: viewAttached,
            title: 'Car Lists'
        };


        return vm;
    });